package thread;

//解决线程安全问题的懒汉模式
//1.加锁
//2.双重if
//3.volatile
class SingletonLazy{
    private static volatile SingletonLazy instance = null;

    public static SingletonLazy getInstance(){
        //instance 如果为null，说明是首次使用，就需要加锁。反之说明是后续的调用，就不用加锁了
        if(instance==null){
            synchronized(SingletonLazy.class){
                if(instance==null){
                    instance=new SingletonLazy();
                }
            }
        }
        return instance;
    }
    private SingletonLazy(){}
}


public class demo1 {
    public static void main(String[] args) {
            SingletonLazy p1 =SingletonLazy.getInstance();
            SingletonLazy p2 =SingletonLazy.getInstance();
            System.out.println(p1==p2);
    }
}
